/*
 * <!--<ul id="selectedCats" class="fn-clear" name="select"></ul>-->
 <!--<select name="cates[]" multiple="multiple" id="cates" size="2" style="display: none;"></select>-->
 */
define(function(require,exports,module){
    require('easyui');
    require('easyui-css');
    require('easyui-icon-css');
    var $ = require("$");
    /*
     * 类目选择
     * */
    function CatSelection(selId,catsId,selCatsWrap,selCatsSelect,multiLimit){
        this.$selId = $("#"+selId);//选择按钮id
        this.$catsId = $("#"+catsId);//所有选择项容器id
        this.$selCatsWrap =$("#"+selCatsWrap);//包含已选项的普通容器Id
        this.$selCatsSelect =$("#"+selCatsSelect);//包含已选项的select容器Id
        this.multiLimit = multiLimit != undefined ? multiLimit : true;
        this.catNum = 0;
    }
    module.exports = CatSelection;//对外提供的接口
    CatSelection.prototype.run = function(){
        var that = this;
        that.initCats();
        that.$selId.click(function(){ that.openCats(); });
        that.selectCat();
        that.deleteCat();
    };
    CatSelection.prototype.openCats = function(){
        var that = this;

        that.$catsId.dialog({
            width:600,
            height:400,
            closed:false,
            left:$('body').width()-650,
            buttons:[{
                text:"确定",
                handler:function(){
                    that.$catsId.dialog('close');
                }
            }]
        });
    };
    CatSelection.prototype.initCats = function(){
        var that = this, catIds = [],max = 0;
        //初始化已选项
        that.$selCatsWrap.children('li').each(function(){
            catIds.push($(this).attr('id'));
        })
        max = catIds.length;
        if(max<=0){
            that.$catsId.find("li:not('.root')").parent('ul').hide();
            that.$catsId.find('input').prop('disabled',false);
            that.$catsId.find("li").each(function(){
                $(this).children("i.minus").removeClass('minus').addClass('plus');
            });
        }else{
            if(that.multiLimit){
                that.$catsId.find("li>input#"+catIds[0]).parents('li.root').siblings('li.root').find('input').prop('disabled',true);
            }
            for(var i = 0;i<max; i++){
                that.$catsId.find("li>input#"+catIds[i]).prop("checked",true);
            }
        };
        //定义列表操作属性
        that.$catsId.find("li").each(function(){
            var $sub = $(this).children("ul"), //子类
                $status = $(this).children("i"), //开关状态
                $input = $(this).children("input");
            if($sub.length>0){
                $status.click(function(){
                    var isOpen = $status.hasClass("minus");
                    if(isOpen){
                        $(this).removeClass('minus').addClass('plus');
                        $sub.hide();
                    }else{
                        $(this).removeClass('plus').addClass('minus');
                        $sub.show();
                    }
                });
            }
            $input.click(function(e){
                var isOpen;
                if($sub.length>0){
                    isOpen = $status.hasClass("minus");
                    if(!isOpen&&$(this).prop('checked')){
                        $status.removeClass('plus').addClass('minus');
                        $sub.show();
                    }
                }
            });
        });
    };
    CatSelection.prototype.selectCat = function(){
        var that = this;
        that.$catsId.find("input").each(function(){
            var $input = $(this);
            $input.click(function(e){
                var $html,$option,
                    catId = $(this).attr("id"),
                    catName = $(this).val()
                that.catNum = that.$selCatsWrap.children("li").length;
                if($(this).prop("checked") && that.catNum<6 ){
                    if(that.catNum <= 0 && that.multiLimit){
                        $(this).parents('li.root').siblings('li.root').find('input').prop('disabled',true);
                    }
                    $html = $("<li id='"+catId+"' class='fn-left fn-clear mr5'><span class='fn-left'>"+catName+"</span>"+
                        "<a href='javascript:void(0);' title='删除此项' class='del fn-left'></a></li>");
                    $option = $("<option id='"+catId+"' value ='"+catId+"' selected = 'selected'>"+catName+"</option>");
                    that.$selCatsWrap.append($html);
                    that.$selCatsSelect.append($option);
                }else if(!$(this).prop("checked")){
                    if(that.catNum==1){that.$catsId.find('input').prop('disabled',false);}
                    that.$selCatsWrap.children("li#"+catId).remove();
                    that.$selCatsSelect.children("option#"+catId).remove();
                }else if(that.catNum>=6){
                    $(this).prop("checked",false);
                    alert("选择类别不能超过6个");
                }
            });
        });
    };

    //删除元素
    CatSelection.prototype.deleteCat = function(){
        var that = this;
        that.$selCatsWrap.hover(function(){
            that.$selCatsWrap.children("li").each(function(){
                var $del = $(this).children(".del");
                $del.click(function(){
                    var catId = $(this).parent("li").attr('id');
                    that.$catsId.find("li").each(function(){
                        var $ipt = $(this).children('input');
                        if($ipt.attr('id') == catId){
                            $ipt.prop("checked",false);
                        }
                    });
                    that.$selCatsWrap.children("li#"+catId).remove();
                    that.$selCatsSelect.children("option#"+catId).remove();
                    if(that.$selCatsWrap.children("li").length<=0){
                        that.$catsId.find("li").each(function(){
                            var $ipt = $(this).children('input');
                            $ipt.prop("disabled",false);
                        });
                    }
                });
            });
        });
    };
});